package com.caseProject.puremvc.mediator
{
	import com.caseProject.puremvc.mediator.events.GraphicEvent;
	import com.caseProject.puremvc.mediator.events.MenuDiagramListEvent;
	import com.caseProject.puremvc.mediator.view.MenuDiagramListView;
	import com.caseProject.puremvc.model.Diagram;
	import com.caseProject.puremvc.notifications.NotificationNames;
	
	import flash.events.Event;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class MenuDiagramListMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "ProjectListMenuMediator";
		
		private var diagramList:Vector.<Diagram>;
		
		public function MenuDiagramListMediator(viewComponent:MenuDiagramListView)
		{
			super(NAME, viewComponent);
			this.diagramList = new Vector.<Diagram>();
			this.menuDiagramListView.addEventListener(MenuDiagramListEvent.SELECT_ITEM, onSelectDiagram);
			this.menuDiagramListView.addEventListener(MenuDiagramListEvent.RENAME_DIAGRAM, onRenameDiagram);
			this.menuDiagramListView.addEventListener(MenuDiagramListEvent.DELETE_DIAGRAM, onDeleteDiagram);
		}
		
		protected function onDeleteDiagram(event:MenuDiagramListEvent):void
		{
			var diagram:Diagram = event.diagramVO as Diagram;
			this.sendNotification(NotificationNames.REMOVE_DIAGRAM, diagram);
		}
		
		protected function onRenameDiagram(event:MenuDiagramListEvent):void
		{
			var diagram:Diagram = event.diagramVO as Diagram;
			this.sendNotification(NotificationNames.MENU_EDIT_RENAME_DIAGRAM,diagram); 
		}
		
		public function get menuDiagramListView():MenuDiagramListView
		{
			return viewComponent as MenuDiagramListView;
		}
		
		protected function onSelectDiagram(event:MenuDiagramListEvent):void
		{
			var diagramVO:Diagram = event.diagramVO;
			this.sendNotification(NotificationNames.MENU_LIST_OPEN_DIAGRAM, diagramVO);		
		}
		
		override public function listNotificationInterests():Array
		{
			return [
						NotificationNames.OPEN_PROJECT_SUCESS,
						NotificationNames.CREATE_DIAGRAM_SUCESS,
						NotificationNames.MENU_EDIT_RENAME_DIAGRAM_SUCCESS,
						NotificationNames.CREATE_PROJECT_SUCESS,
						NotificationNames.CLOSE_PROJECT_SUCESS
					];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch (notification.getName())
			{
				case NotificationNames.CLOSE_PROJECT_SUCESS:
					this.menuDiagramListView.deleteLabels();				
					break;
				
				case NotificationNames.CREATE_PROJECT_SUCESS:
					this.menuDiagramListView.deleteLabels();				
					break;
				
				case NotificationNames.OPEN_PROJECT_SUCESS:
					this.diagramList = notification.getBody() as Vector.<Diagram>;
					this.menuDiagramListView.deleteLabels();
					this.menuDiagramListView.setLabels(this.diagramList);					
					break;
				
				case NotificationNames.CREATE_DIAGRAM_SUCESS:
					this.diagramList = notification.getBody() as Vector.<Diagram>;
					this.menuDiagramListView.addLabel(this.diagramList);
					break;
				case NotificationNames.MENU_EDIT_RENAME_DIAGRAM_SUCCESS:
					this.menuDiagramListView.deleteLabels();
					this.menuDiagramListView.setLabels(this.diagramList);
					break;
			}			
		}
	}
}